package rmhc.api.fb
{
	import com.facebook.graph.Facebook;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.system.Security;
	
	import rmhc.api.ApiWrapper;
	import rmhc.api.dataflow.UserInfo;
	import rmhc.api.dataflow.WallPostInfo;
	import rmhc.api.tasks.BaseApiTask;
	import rmhc.api.tasks.IApiTask;
	import rmhc.api.tasks.IGetFriends;
	import rmhc.api.tasks.IGetProfilePicture;
	import rmhc.api.tasks.IPrepareProfileUpload;
	import rmhc.api.tasks.ISetProfilePicture;
	import rmhc.api.tasks.IWallPost;
	import rmhc.gui.SupplyFunctions;
	
	public class FbApi extends ApiWrapper
	{
		protected const PERMS:Array = [ "publish_stream" , "user_photos" , "friends_photos" , "photo_upload" ];
		protected const APP_ID:String = "185435194873055";
		
		protected var token:String;
		protected var sig:String;
		protected var me:UserInfo;
		
		public function FbApi( params:Object )
		{
			super();
			token = params.token;
			sig = params.sig;
			trace( "FbApi token" , token );
		}
		
		override public function connect():void
		{
			Security.allowDomain( "*" );
			//Security.allowDomain( "*.facebook.com" );
			Security.allowInsecureDomain( "*" );
			Facebook.init( APP_ID , onInit , null , token );
			trace( "FbApi init" );
		}
		
		protected function onInit( result:Object , fail:Object ):void
		{
			trace( "onInit" , result , fail );
			if ( result )//&& checkPerms( result.availablePermissions ) )
			{
				trace( "FB.init success" );
				//загружаем данные юзера
				var mi:GetUserInfo = new GetUserInfo( this , "me" );
				//addTask( mi );
				mi.addEventListener( Event.COMPLETE, onMyInfo );
				mi.run();
				//getUserInfo( onMyInfo , "me" );
			}
			else
			{
				trace( "FB.init fail" );
				var opts:Object = { perms : PERMS.join( ", " ) };
				Facebook.login( onInit,  opts );
			}
		}
		protected function onMyInfo( e:Event ):void
		{
			var mi:GetUserInfo = e.target as GetUserInfo;
			me = mi.getInfo();
			trace( "me" , me.firstname );
			dispatchEvent( new Event( Event.CONNECT ) );
		}
		
		
		
		override public function prepareProfileUpload():IPrepareProfileUpload
		{
			return new PrepareDummyTask( this );
		}
		override public function preparePostToWall( ):IApiTask
		{
			return new PrepareDummyTask( this );
		}
		
		
		override public function getProfilePicture( profileId:String ):IGetProfilePicture
		{
			if ( profileId == null )
			{
				profileId = "me";
			}
			var t:GetProfilePicture = new GetProfilePicture( this , profileId );
			//addTask( t );
			return  t;
		}
		override public function setProfilePicture( picture:BitmapData ):ISetProfilePicture
		{
			var t:SetProfilePicture = new SetProfilePicture(this, picture);
			return  t;
		}
		override public function getFriends():IGetFriends
		{
			var t:GetFriends = new GetFriends( this );
			//addTask( t );
			return t;
		}
		override public function postToWall( post:WallPostInfo ):IWallPost
		{
			if ( !post.wallId )
			{
				post.wallId = "me";
			}
			var t:PostToWall = new PostToWall( this , post );
			//addTask( t );
			return  t;
		}
		
		override public function getAppUrl():String
		{
			return "http://apps.facebook.com/mchappyday_ru/";
		}
		override public function getLadoshkId():String
		{
			//return "http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc7/390360_258849010829172_100001122254332_661196_1644399962_n.jpg";
			return "http://mind2create.ru/fb/rmhc/ladoshka_big.png";
		}
		override public function getNetworkId():int
		{
			return FB;
		}
		override public function getUID():String
		{
			return me.uid;
		}
		override public function getMe():UserInfo 
		{
			return me;
		}
		
		override public function runTask( t:BaseApiTask ):Boolean
		{
			if ( !super.runTask( t ) )
			{
				return false;
			}
			callTask( t as FbApiTask );
			return true;
		}
		protected function callTask( t:FbApiTask ):void
		{
			trace( "call task" , t.getTarget() , SupplyFunctions.objectToString( t.getParams() ) );
			Facebook.api( t.getTarget() , t.parseData , t.getParams() , t.getMethod() );
		}
	}
}